package com.gexiaobao.pigeon.ui.fragment.mine.banking

import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import androidx.fragment.app.setFragmentResultListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.param.UnBindBankBean
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentBankCardBinding
import com.gexiaobao.pigeon.ui.adapter.BankListAdapter
import com.gexiaobao.pigeon.viewmodel.MineBankingViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 19:12
 * @Describe    : 我的银行卡
 */
class FragmentBankCard : BaseFragment<MineBankingViewModel, FragmentBankCardBinding>() {

    private val mAdapter: BankListAdapter by lazy { BankListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeTitleBarMenu.toolbar.initClose("我的银行卡") {
            nav().navigateUp()
        }
        mDatabind.includeTitleBarMenu.tvRightMenu.text = "添加银行卡"

        val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
            val width = resources.getDimensionPixelSize(R.dimen.dp_70)
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val deleteItem = SwipeMenuItem(activity)
                .setBackground(R.drawable.round_bg5_delete_red_half)
                .setTextColor(Color.WHITE)
                .setText(R.string.delete)
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(deleteItem)
        }
        mDatabind.layoutList.swipeRecyclerView.setSwipeMenuCreator(swipeMenuCreator)

        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
            /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
            menuBridge.closeMenu()
            /** 左侧还是右侧菜单 */
//            val direction = menuBridge.direction
//            /**  菜单在Item中的Position */
//            val menuPosition = menuBridge.position
            /** 调用删除接口 */
            showLoading(getString(R.string.loading))
            val body = UnBindBankBean(
                carNo = mAdapter.data[position].bankCardNo
            )
            mViewModel.unbindBankCard(body)
        }
        mDatabind.layoutList.swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener)

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )

        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.queryBankCard()
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.queryBankCard()
        }

        setFragmentResultListener("ok") { _, bundle ->
            if (bundle.getBoolean("isSuccess")) {
                lazyLoadData()
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.queryBankCard()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.unBindBankResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                mViewModel.queryBankCard()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }

        mViewModel.queryBankListResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeTitleBarMenu.tvRightMenu) {
            when (it) {
                mDatabind.includeTitleBarMenu.tvRightMenu -> {
                    nav().navigateAction(R.id.action_to_add_bank_card)
                }
            }
        }
    }
}